home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
usenet
/
sources
/
volume91
/
aplictns
/
mandset1
/
part01
/
scanfloat.c
< prev
Wrap
C/C++ Source or Header
|
1991-05-08
|
1KB
|
50 lines
/* Routine to scan a floating point number from a null-terminated string.
* Written 1987 by Russell Wallace. May be freely copied and used in
* developing Amiga software. Requires linkage with m.lib if used with
* Aztec C. */
typedef double TYPE; /* Define this as float or double, whichever reqd. */
TYPE scanfloat (string)
char *string;
{
register short i,j,minus;
TYPE number,column;
i=minus=0;
while ((string[i]<'0' || string[i]>'9')&& string[i]!='-' && string[i]!='.' && string[i])
i++; /* Search for start of number or end of string */
if (string[i]=='-')
{
minus=1; /* Is negative number */
i++;
if ((string[i]<'0' || string[i]>'9')&& string[i]!='.')
return ((TYPE)0);
}
if (string[i]=='\0')
return ((TYPE)0); /* If end of string, return 0 */
string+=i; /* Move along to start of string */
i=0;
while (string[i]>='0' && string[i]<='9')
i++; /* Look for decimal point */
j=i;
number=0;
column=1;
for (i--;i>=0;i--) /* Go back from point to start of number */
{
number+=(string[i]-'0')*column; /* add digit value to number */
column*=10; /* each column is 10 times value of one to right */
}
column=.1;
if (string[j]=='.') /* If decimal point, get stuff after it */
for (i=j+1;string[i]>='0' && string[i]<='9';i++)
{ /* scan right to end of number */
number+=(string[i]-'0')*column;
column/=10; /* each column 10 times less than one to left */
}
if (minus)
return (-number);
else
return (number);
}